﻿using System;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Generic;
using System.Text;

using Model;
namespace DAL
{
    #region class SQLDALSuccessInfo
    /// <summary>
    /// it is suitable to the table with only one primary key,and can not deal with the aotu-increment key.
    /// Note:the first field in the table must be primary and autoincrement and int ,if exists 
    /// class:DAL SuccessInfo 
    /// Author:wxc
    /// DateTime:2021/5/26 19:36:48
    /// </summary>	
    public class DALSuccessInfo
    {
        #region SQL CONST		
        private const string INSERT_SQL = "INSERT INTO [SuccessInfo] ([ID],[Collage1],[Collage2],[Collage3],[Collage4],[Collage5]) Values (@ID,@Collage1,@Collage2,@Collage3,@Collage4,@Collage5)";
        private const string UPDATE_SQL = "UPDATE [SuccessInfo] SET Collage1=@Collage1,Collage2=@Collage2,Collage3=@Collage3,Collage4=@Collage4,Collage5=@Collage5 WHERE [ID]=@ID";
        private const string DEL_SQL = "DELETE FROM [SuccessInfo] WHERE [ID]=@ID";
        private const string SELECT_ALL_SQL = "SELECT [ID],[Collage1],[Collage2],[Collage3],[Collage4],[Collage5] FROM [SuccessInfo] order by iD desc";
        private const string SELECT_ONE_SQL = "SELECT [ID],[Collage1],[Collage2],[Collage3],[Collage4],[Collage5] FROM [SuccessInfo] WHERE [ID]=@ID";
        private const string SELECT_WHERE_SQL = "SELECT [ID],[Collage1],[Collage2],[Collage3],[Collage4],[Collage5] FROM [SuccessInfo] {$where} order by  iD desc";
        #endregion
        #region Paras const
        private const string PARA_ID = "@ID";
        private const string PARA_Collage1 = "@Collage1";
        private const string PARA_Collage2 = "@Collage2";
        private const string PARA_Collage3 = "@Collage3";
        private const string PARA_Collage4 = "@Collage4";
        private const string PARA_Collage5 = "@Collage5";
        #endregion
        #region Add Method
        /// <summary>
        /// Insert an item
        /// </summary>
        /// <param name="SuccessInfo"></param>
        /// <returns>count of rows affected</returns>
        public int AddSuccessInfo(SuccessInfoEntity SuccessInfo)
        {
            int backvalue = 0;
            //get paras
            SqlParameter[] SuccessInfoParms = GetSuccessInfoParametersInsert();
            //set values to paras
            SuccessInfoParms[0].Value = SuccessInfo.ID;
            SuccessInfoParms[1].Value = SuccessInfo.Collage1;
            SuccessInfoParms[2].Value = SuccessInfo.Collage2;
            SuccessInfoParms[3].Value = SuccessInfo.Collage3;
            SuccessInfoParms[4].Value = SuccessInfo.Collage4;
            SuccessInfoParms[5].Value = SuccessInfo.Collage5;
            //insert			
            using (SqlConnection conn = new SqlConnection(SQLHelper.ConnectionString))
            {
                backvalue = SQLHelper.ExecuteNonQuery(SQLHelper.ConnectionString, CommandType.Text, INSERT_SQL, SuccessInfoParms);
            }
            return backvalue;
        }
        #endregion
        #region Mod Method
        /// <summary>
        /// Update an item
        /// </summary>
        /// <param name="SuccessInfo"></param>
        /// <returns>count of rows affected</returns>
        public int ModSuccessInfo(SuccessInfoEntity SuccessInfo)
        {
            int backvalue = 0;
            //get paras
            SqlParameter[] SuccessInfoParms = GetSuccessInfoParameters();
            //set values to paras
            SuccessInfoParms[0].Value = SuccessInfo.ID;
            SuccessInfoParms[1].Value = SuccessInfo.Collage1;
            SuccessInfoParms[2].Value = SuccessInfo.Collage2;
            SuccessInfoParms[3].Value = SuccessInfo.Collage3;
            SuccessInfoParms[4].Value = SuccessInfo.Collage4;
            SuccessInfoParms[5].Value = SuccessInfo.Collage5;
            //update			
            using (SqlConnection conn = new SqlConnection(SQLHelper.ConnectionString))
            {
                backvalue = SQLHelper.ExecuteNonQuery(SQLHelper.ConnectionString, CommandType.Text, UPDATE_SQL, SuccessInfoParms);
            }
            return backvalue;
        }
        #endregion
        #region Del Method
        /// <summary>
        /// Del an item by primary key
        /// </summary>
        /// <param name="iD"></param> 
        /// <returns>count of rows affected</returns>
        public int DelSuccessInfo(string iD)
        {
            int backvalue = 0;
            //primarykeys
            //get paras
            SqlParameter[] SuccessInfoParms = GetSuccessInfoPrimaryKeyParameters();
            //set values to paras
            SuccessInfoParms[0].Value = iD;
            //delete
            using (SqlConnection conn = new SqlConnection(SQLHelper.ConnectionString))
            {
                backvalue = SQLHelper.ExecuteNonQuery(SQLHelper.ConnectionString, CommandType.Text, DEL_SQL, SuccessInfoParms);
            }
            return backvalue;
        }
        #endregion
        #region Select Method
        /// <summary>
        /// Select an item by Primarykey
        /// </summary>
        /// <param name="iD"></param>
        /// <returns>an item</returns>
        public SuccessInfoEntity GetSuccessInfo(string iD)
        {
            SuccessInfoEntity SuccessInfo = new SuccessInfoEntity();
            //primarykeys
            //get paras
            SqlParameter[] SuccessInfoParms = GetSuccessInfoPrimaryKeyParameters();
            //set values to paras
            SuccessInfoParms[0].Value = iD;
            //select
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.ConnectionString, CommandType.Text, SELECT_ONE_SQL, SuccessInfoParms))
            {
                if (reader.Read())
                {
                    if (!reader.IsDBNull(0)) SuccessInfo.ID = reader.GetString(0);
                    if (!reader.IsDBNull(1)) SuccessInfo.Collage1 = reader.GetString(1);
                    if (!reader.IsDBNull(2)) SuccessInfo.Collage2 = reader.GetString(2);
                    if (!reader.IsDBNull(3)) SuccessInfo.Collage3 = reader.GetString(3);
                    if (!reader.IsDBNull(4)) SuccessInfo.Collage4 = reader.GetString(4);
                    if (!reader.IsDBNull(5)) SuccessInfo.Collage5 = reader.GetString(5);
                }
                else
                {
                    SuccessInfo = null;
                }
            }
            return SuccessInfo;

        }
        #endregion
        #region Select_All Method
        /// <summary>
        /// Select all items
        /// </summary>
        /// <returns>items</returns>
        public IList<SuccessInfoEntity> GetSuccessInfos()
        {
            IList<SuccessInfoEntity> SuccessInfos = new List<SuccessInfoEntity>();
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.ConnectionString, CommandType.Text, SELECT_ALL_SQL, null))
            {
                while (reader.Read())
                {
                    SuccessInfoEntity SuccessInfo = new SuccessInfoEntity();
                    if (!reader.IsDBNull(0)) SuccessInfo.ID = reader.GetString(0);
                    if (!reader.IsDBNull(1)) SuccessInfo.Collage1 = reader.GetString(1);
                    if (!reader.IsDBNull(2)) SuccessInfo.Collage2 = reader.GetString(2);
                    if (!reader.IsDBNull(3)) SuccessInfo.Collage3 = reader.GetString(3);
                    if (!reader.IsDBNull(4)) SuccessInfo.Collage4 = reader.GetString(4);
                    if (!reader.IsDBNull(5)) SuccessInfo.Collage5 = reader.GetString(5);
                    SuccessInfos.Add(SuccessInfo);
                }
            }
            return SuccessInfos;
        }
        #endregion
        #region private common select by conditions
        /// <summary>
        /// private common select by conditions
        /// </summary>
        /// <returns>IList<SuccessInfoEntity></returns>
        private IList<SuccessInfoEntity> GetSuccessInfosbyQueryString(string SQLstr)
        {
            IList<SuccessInfoEntity> SuccessInfos = new List<SuccessInfoEntity>();
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.ConnectionString, CommandType.Text, SQLstr))
            {
                while (reader.Read())
                {
                    SuccessInfoEntity SuccessInfo = new SuccessInfoEntity();
                    if (!reader.IsDBNull(0)) SuccessInfo.ID = reader.GetString(0);
                    if (!reader.IsDBNull(1)) SuccessInfo.Collage1 = reader.GetString(1);
                    if (!reader.IsDBNull(2)) SuccessInfo.Collage2 = reader.GetString(2);
                    if (!reader.IsDBNull(3)) SuccessInfo.Collage3 = reader.GetString(3);
                    if (!reader.IsDBNull(4)) SuccessInfo.Collage4 = reader.GetString(4);
                    if (!reader.IsDBNull(5)) SuccessInfo.Collage5 = reader.GetString(5);
                    SuccessInfos.Add(SuccessInfo);
                }
            }
            return SuccessInfos;
        }
        #endregion
        #region public common select by conditions ,we can use it by changing the GetSuccessInfosbyCondition(datatype condition) and sql
        /// <summary>
        /// Select items by Columns
        /// </summary>
        /// <returns>IList<SuccessInfoEntity></returns>
        public IList<SuccessInfoEntity> GetSuccessInfosbyCondition(string sql)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(SELECT_WHERE_SQL);
            sb.Replace("{$where}", "where " + sql);
            return GetSuccessInfosbyQueryString(sb.ToString());
        }
        #endregion
        #region Select Method Top n
        /// <summary>
        /// Select items by Columns
        /// </summary>
        /// <returns>IList<SuccessInfoEntity></returns>
        public IList<SuccessInfoEntity> GetSuccessInfosTopN(int n)
        {
            IList<SuccessInfoEntity> SuccessInfos = new List<SuccessInfoEntity>();
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.ConnectionString, CommandType.Text, SELECT_ALL_SQL, null))
            {
                int i = 0;
                while (reader.Read() && i < n)
                {
                    SuccessInfoEntity SuccessInfo = new SuccessInfoEntity();
                    if (!reader.IsDBNull(0)) SuccessInfo.ID = reader.GetString(0);
                    if (!reader.IsDBNull(1)) SuccessInfo.Collage1 = reader.GetString(1);
                    if (!reader.IsDBNull(2)) SuccessInfo.Collage2 = reader.GetString(2);
                    if (!reader.IsDBNull(3)) SuccessInfo.Collage3 = reader.GetString(3);
                    if (!reader.IsDBNull(4)) SuccessInfo.Collage4 = reader.GetString(4);
                    if (!reader.IsDBNull(5)) SuccessInfo.Collage5 = reader.GetString(5);
                    SuccessInfos.Add(SuccessInfo);
                    i++;
                }
            }
            return SuccessInfos;
        }
        #endregion
        #region paras configuration
        /// <summary>
        /// Internal function to get cached parameters:all paras --- suitable to insert ,especially autoincrement
        /// </summary>
        /// <returns></returns>
        private static SqlParameter[] GetSuccessInfoParametersInsert()
        {
            SqlParameter[] parms = SQLHelper.GetCachedParameters("SQL_ALL_SuccessInfo_Insert");

            if (parms == null)
            {
                parms = new SqlParameter[] {
            new SqlParameter(PARA_ID,SqlDbType.NVarChar) ,
            new SqlParameter(PARA_Collage1,SqlDbType.NVarChar) ,
            new SqlParameter(PARA_Collage2,SqlDbType.NVarChar) ,
            new SqlParameter(PARA_Collage3,SqlDbType.NVarChar) ,
            new SqlParameter(PARA_Collage4,SqlDbType.NVarChar) ,
            new SqlParameter(PARA_Collage5,SqlDbType.NVarChar)          };
                SQLHelper.CacheParameters("SQL_ALL_SuccessInfo_Insert", parms);
            }
            return parms;
        }
        /// <summary>
        /// Internal function to get cached parameters:all paras --- suitable to update
        /// </summary>
        /// <returns></returns>
        private static SqlParameter[] GetSuccessInfoParameters()
        {
            SqlParameter[] parms = SQLHelper.GetCachedParameters("SQL_ALL_SuccessInfo_Update");

            if (parms == null)
            {
                parms = new SqlParameter[] {
                    new SqlParameter(PARA_ID,SqlDbType.NVarChar) ,
                    new SqlParameter(PARA_Collage1,SqlDbType.NVarChar) ,
                    new SqlParameter(PARA_Collage2,SqlDbType.NVarChar) ,
                    new SqlParameter(PARA_Collage3,SqlDbType.NVarChar) ,
                    new SqlParameter(PARA_Collage4,SqlDbType.NVarChar) ,
                    new SqlParameter(PARA_Collage5,SqlDbType.NVarChar)                  };
                SQLHelper.CacheParameters("SQL_ALL_SuccessInfo_Update", parms);
            }
            return parms;
        }
        /// <summary>
        /// Internal function to get cached parameters:primary key paras
        /// </summary>
        /// <returns></returns>
        private static SqlParameter[] GetSuccessInfoPrimaryKeyParameters()
        {
            SqlParameter[] parms = SQLHelper.GetCachedParameters("SQL_PrimaryKey_SuccessInfo");

            if (parms == null)
            {
                parms = new SqlParameter[] {
                    new SqlParameter(PARA_ID,SqlDbType.NVarChar) ,
                    };
                SQLHelper.CacheParameters("SQL_PrimaryKey_SuccessInfo", parms);
            }
            return parms;
        }
        #endregion
    }
    #endregion
}
